trigger trg_accountActivity on Account_Activity__c (after insert, after update) 
{
    Map<ID,string> accountIdMap = new Map<ID,string>();
    Map<ID,Account_Activity__c> mapOfAccountActivityObject=new Map<ID,Account_Activity__c> ([Select id,Account__r.Activity__c from Account_Activity__c where id in: Trigger.New]);

    for(Account_Activity__c accountActivityObject :trigger.New)
    {
        
        accountIdMap.put(accountActivityObject.Account__c,mapOfAccountActivityObject.get(accountActivityObject.Id).Account__r.Activity__c); 
    
    }
   
    System.debug('The Account Id Map => '+accountIdMap.size() + ' & The Values => '+ accountIdMap.values());
    if(accountIdMap.size() > 0)
    {
        //For Restrict user to add the activity...
        //accountActivityObjectList = new List<Account_Activity__c>([Select COUNT()]);
        AggregateResult[] groupedResults = [SELECT COUNT(Id),Account__c FROM Account_Activity__c where Account__c IN : accountIdMap.keyset() AND Active__c = true GROUP BY Account__c ];
         
        for (AggregateResult ar : groupedResults) 
        {
            
            System.debug('The Account Activity Value => '+accountIdMap.get(String.valueOf(ar.get('Account__c'))) + ' & The Count => '+ ar.get('expr0'));
            System.debug('The Count => '+ ar.get('expr0'));
            
            for(Account_Activity__c accountActivityObject :trigger.New)
            {
                if(accountActivityObject.Account__c == ar.get('Account__c'))
                {
                    System.debug('The Process => '+ accountActivityObject.Account__c);
                    if(mapOfAccountActivityObject.get(accountActivityObject.Id).Account__r.Activity__c == 'Trading - One Activity' && Integer.valueOf(ar.get('expr0')) > 1)
                        accountActivityObject.Activity__c.adderror('Please Check Company Activity Previously Selected');
                    else if(mapOfAccountActivityObject.get(accountActivityObject.Id).Account__r.Activity__c == 'Trading - Two Activity' && Integer.valueOf(ar.get('expr0')) > 2)
                        accountActivityObject.Activity__c.adderror('Please Check Company Activity Previously Selected');    
                    else if(mapOfAccountActivityObject.get(accountActivityObject.Id).Account__r.Activity__c == 'Trading - Three Activity' && Integer.valueOf(ar.get('expr0')) > 3)
                        accountActivityObject.Activity__c.adderror('Please Check Company Activity Previously Selected');///
                }
                    
            }
            
        }
        
     
    }
    
    
    
    

}